var Handler_inter = null;
var viewPoint = null;
function IntervisibilityOnload() {
    var tooltip = new GlobeTooltip(viewer.container);
    viewer.scene.globe.depthTestAgainstTerrain = true;
    Handler_inter = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    // 观察点
    // var viewPoint = null;
    // 目标点
    var viewPoints = null;
    var targetPoints = [];
    var objectsToExclude = [];
    var viewPointEntity;
    $('#Intervisibility_chooseView').click(function() {
        removeAllEntities();
        viewPoint = null
        Handler_inter.setInputAction(function(movement) {
            targetPoints = [];
            position = getPickPositionGlobe(movement.position);
            viewPointEntity = drawPoint(position);
            viewPoint = position;
            // 视角位置创建坐标轴
            // var transform = Cesium.Transforms.eastNorthUpToFixedFrame(viewPoint);
            // var modelMatrixPrimitive = viewer.scene.primitives.add(new Cesium.DebugModelMatrixPrimitive({
            //     modelMatrix: transform,
            //     length: 10.0
            // }));
            objectsToExclude = [viewPointEntity];
            tooltip.setVisible(false);
            Handler_inter.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

        //鼠标移动
        Handler_inter.setInputAction(function(event) {
            var wp = event.endPosition;
            if (!Cesium.defined(wp)) {
                return;
            }
            if (viewPoint == null) {
                tooltip.showAt(wp, "<p>单击选择观察点</p>");
            }
            var position = getPickPositionGlobe(wp);
            if (!Cesium.defined(position)) {
                return;
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    });

    $("#Intervisibility_addPoint").click(function() {
        if(viewPoint == null){
            layui.layer.msg("请先选择观察点！",{icon:5});
        }else{
            viewPoints = null
        Handler_inter.setInputAction(function(movement) {
            position = getPickPositionGlobe(movement.position);
            destPointEntity = drawPoint(position);
            viewPoints = position;
            targetPoints.push(position);
            tooltip.setVisible(false);
            Handler_inter.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
            pickFromRay();
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        //鼠标移动
        Handler_inter.setInputAction(function(event) {
            var wp = event.endPosition;
            if (!Cesium.defined(wp)) {
                return;
            }
            if (viewPoints == null) {
                tooltip.showAt(wp, "<p>单击选择目标点</p>");
            }
            var position = getPickPositionGlobe(wp);
            if (!Cesium.defined(position)) {
                return;
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        }
        
    });

    $("#Intervisibility_clear").click(function() {
        targetPoints = [];
        Intervisibility_clear();
    });

    function pickFromRay() {
        if (!viewPoint || !viewPoints) {
            showMsgLayer("观察点或目标点为空");
            return;
        }
        var direction = Cesium.Cartesian3.normalize(Cesium.Cartesian3.subtract(targetPoints[targetPoints.length - 1], viewPoint, new Cesium.Cartesian3()), new Cesium.Cartesian3());
        // 建立射线
        var ray = new Cesium.Ray(viewPoint, direction);
        var result = viewer.scene.pickFromRay(ray, objectsToExclude); // 计算交互点，返回第一个
        showIntersection(result, targetPoints[targetPoints.length - 1], viewPoint);
    }

    // 处理交互点
    function showIntersection(result, destPoint, viewPoint) {
        // 如果是场景模型的交互点，排除交互点是地球表面
        if (Cesium.defined(result) && Cesium.defined(result.object) && Cesium.defined(result.position)) {
            drawLine(result.position, viewPoint, Cesium.Color.GREEN); // 可视区域
            drawLine(result.position, destPoint, Cesium.Color.RED); // 不可视区域
        } else {
            drawLine(viewPoint, destPoint, Cesium.Color.GREEN);
        }
    }

    // 绘制点
    function drawPoint(position, config) {
        var pointGeometry = viewer.entities.add({
            position: position,
            point: {
                color: Cesium.Color.RED,
                pixelSize: 10
            }
        });
        return pointGeometry;
    };

    // 绘制线
    function drawLine(leftPoint, secPoint, color) {
        viewer.entities.add({
            polyline: {
                positions: [leftPoint, secPoint],
                arcType: Cesium.ArcType.RHUMB,
                width: 3,
                material: color,
                depthFailMaterial: color,
                classificationType: Cesium.ClassificationType.TERRAIN,
                //clampToGround: true
            }
        })
    }
}

// 清除通视分析
function Intervisibility_clear(clear) {
    Handler_inter && Handler_inter.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK && Handler_inter.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE));
    if (!clear) { 
        removeAllEntities();
        viewPoint = null;
    }
}